package com.example.administrator.znjt.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;

import com.example.administrator.znjt.R;

/**
 * Created by Administrator on 2017/5/10.
 */

public class WelcomeActivity extends Activity{

    private boolean isFirst=false;

    private static final int TIME=1000;
    private static final int GUIDE_TIME=1001;
    private static final int LOGIN_TIME=1002;

    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;

    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case GUIDE_TIME:
                    Guide();
                    break;
                case LOGIN_TIME:
                    Login();
                    break;
            }
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.welcome);
        sharedPreferences=getSharedPreferences("First",MODE_PRIVATE);
        init();
    }

    private void init(){
        isFirst=sharedPreferences.getBoolean("First",true);
        if (isFirst){
            editor=sharedPreferences.edit();
            handler.sendEmptyMessageDelayed(GUIDE_TIME,TIME);
            editor.putBoolean("First",false);
            editor.commit();
        }else {
            handler.sendEmptyMessageDelayed(LOGIN_TIME,TIME);
        }
    }

    private void Guide(){
        Intent guide=new Intent(WelcomeActivity.this,GuideActivity.class);
        startActivity(guide);
        finish();
    }

    private void Login(){
        Intent login=new Intent(WelcomeActivity.this,LoginActivity.class);
        startActivity(login);
        finish();
    }

}
